deferred class CD_CANVAS_WORLD_COORDINATES
-- Allows the use of a World Coordinate System. In this system you can 
-- attribute coordinates to any unit you want. After you define a window 
-- (rectangular region) in your world, each given coordinate is then mapped to 
-- canvas coordinates to draw the primitives. You can define a viewport in your 
-- canvas to change the coordinate mapping from world to canvas.
--
-- If you want to map coordinates from one system to another, use the 
-- "wd_world_to_canvas" e "wd_canvas_to_world" functions.
--
-- The quality of the picture depends on the conversion from World to Canvas, 
-- so if the canvas has a small size the picture quality will be poor. To 
-- increase picture quality create a canvas with a larger size, if possible.
--
-- All World Coordinate drawing in all drivers are simulated using other CD 
-- primitives and do NOT depend or use the "set_transform" transformation 
-- matrix.	

inherit
	CANVAS_DRAW

feature {ANY}

	set_window (xmin, xmax, ymin, ymax: REAL_64)
		-- Configures a window in the world coordinate system to be used to 
		-- convert world coordinates (with values in real numbers) into canvas 
		-- coordinates (with values in integers). The default window is the size 
		-- in millimeters of the whole canvas.
		do
			int_wd_canvas_window(cnvs, xmin, xmax, ymin, ymax)
		end

	get_window: TUPLE[REAL_64, REAL_64, REAL_64, REAL_64]
		-- Queries the current window in the world coordinate system being 
		-- used to convert world coordinates into canvas coordinates (and the 
		-- other way round).
		local
			xmin, xmax, ymin, ymax: REAL_64
		do
			int_wd_canvas_get_window(cnvs, $xmin, $xmax, $ymin, $ymax)

			Result := [xmin, xmax, ymin, ymax]
		end

	set_viewport (xmin, xmax, ymin, ymax: INTEGER)
		-- Configures a viewport in the canvas coordinate system to be used to 
		-- convert world coordinates (with values in real numbers) into canvas 
		-- coordinates (with values in integers). The default viewport is the 
		-- whole canvas (0,w-1,0,h-1). If the canvas size is changed, the 
		-- viewport will not be automatically updated. 
		do
			int_wd_canvas_viewport(cnvs, xmin, xmax, ymin, ymax)
		end

	get_viewport: TUPLE[INTEGER, INTEGER, INTEGER, INTEGER]
		-- Queries the current viewport in the world coordinate system being used 
		-- to convert world coordinates into canvas coordinates (and the other 
 		-- way round).
		local
			xmin, xmax, ymin, ymax: INTEGER
		do
			int_wd_canvas_get_viewport(cnvs, $xmin, $xmax, $ymin, $ymax)

			Result := [xmin, xmax, ymin, ymax]
		end

	world_to_canvas (xw, yw: REAL_64): TUPLE[INTEGER, INTEGER]
		-- Converts world coordinates into canvas coordinates.
		local
			xv, yv: INTEGER
		do
			int_wd_canvas_world_to_canvas(cnvs, xw, yw, $xv, $yv)

			Result := [xv, yv]
		end

	canvas_to_world (xv, yv: INTEGER): TUPLE[REAL_64, REAL_64]
		-- Converts canvas coordinates into world coordinates. 
		local
			xw, yw: REAL_64
		do
			int_wd_canvas_canvas_to_world(cnvs, xv, yv, $xw, $yw)

			Result := [xw, yw]
		end

	set_wd_transform (sx, sy, tx, ty: REAL_64)
		-- Configures the world coordinate system transformation to be used to 
		-- convert world coordinates (with values in real numbers) into canvas 
		-- coordinates (with values in integers). The transformation is 
		-- automatically set by "set_window" and "set_viewport". This has NO 
		-- relation with "set_transform".
		do
			int_wd_canvas_set_transform(cnvs, sx, sy, tx, ty)
		end

	get_wd_transform: TUPLE[REAL_64, REAL_64, REAL_64, REAL_64]
		-- Queries the current transformation being used to convert world 
		-- coordinates into canvas coordinates (and the other way round).
		local
			sx, sy, tx, ty: REAL_64
		do
			int_wd_canvas_get_transform(cnvs, $sx, $sy, $tx, $ty)

			Result := [sx, sy, tx, ty]
		end

	set_wd_translate (dtx, dty: REAL_64)
		-- Translates the transformation by a delta, by adding the given values 
		-- to the current tx and ty values.
		do
			int_wd_canvas_translate(cnvs, dtx, dty)
		end

	set_wd_scale (dsx, dsy: REAL_64)
		-- Scales the transformation by a delta, by multiplying the given values 
		-- by the current sx and sy values.
		do
			int_wd_canvas_scale(cnvs, dsx, dsy)
		end

	-- Extra

	wd_hard_copy
		do
			-- Not sure how add this feature.
		end


feature {}

	int_wd_canvas_window (wgt: POINTER; xmin, xmax, ymin, ymax: REAL_64)
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "wdCanvasWindow"
         }"
      end

	int_wd_canvas_get_window (wgt, xmin, xmax, ymin, ymax: POINTER)
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "wdCanvasGetWindow"
         }"
      end

	int_wd_canvas_viewport (wgt: POINTER; xmin, xmax, ymin, ymax: INTEGER)
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "wdCanvasViewport"
         }"
      end

	int_wd_canvas_get_viewport (wgt, xmin, xmax, ymin, ymax: POINTER)
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "wdCanvasGetViewport"
         }"
      end

	int_wd_canvas_world_to_canvas (wgt: POINTER; xw, yw: REAL_64; xv, yv: POINTER)
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "wdCanvasWorld2Canvas"
         }"
      end

	int_wd_canvas_canvas_to_world (wgt: POINTER; xv, yv: INTEGER; xw, yw: POINTER)
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "wdCanvasCanvas2World"
         }"
      end

	int_wd_canvas_set_transform (wgt: POINTER; sx, sy, tx, ty: REAL_64)
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "wdCanvasSetTransform"
         }"
      end

	int_wd_canvas_get_transform (wgt, sx, sy, tx, ty: POINTER)
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "wdCanvasGetTransform"
         }"
      end

	int_wd_canvas_translate (wgt: POINTER; dtx, dty: REAL_64)
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "wdCanvasTranslate"
         }"
      end

	int_wd_canvas_scale (wgt: POINTER; dsx, dsy: REAL_64)
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "wdCanvasScale"
         }"
      end

end

-- The MIT License (MIT)

-- Copyright (c) 2017 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
